//
//  XMFriendListViewController.m
//  XMJJ_zmy
//
//  Created by 丁浩 on 2025/7/7.
//

#import "XMFriendListViewController.h"
#import "XMFriendListView.h"
#import "XMNetwork+mine.h"
#import "XMFriendModel.h"
#import "XMNetwork+home.h"

#import "XMChatViewController.h"

@interface XMFriendListViewController ()

@property (nonatomic, strong) XMFriendListView *friendListView;

@property (nonatomic, assign) NSInteger page;

@end

@implementation XMFriendListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatUI];
}

- (void)creatUI {
    [self.view addSubview:self.friendListView];
    _page = 1;
    [self getFriendList];
}

- (void)getFriendList {
    XMWeakSelf;
    [_Network getFriendListWithPageNum:[NSString stringWithFormat:@"%ld", _page]
                               success:^(id _Nonnull responseObject) {
        NSArray *friendModelArr = [NSArray yy_modelArrayWithClass:[XMFriendModel class] json:[responseObject[@"data"] safeArrayForKey:@"records"]];
        [weakSelf.friendListView setContentWithFriendModelArr:friendModelArr page:weakSelf.page];
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)shareToUserWithUserId:(NSString *)userId
                     nickName:(NSString *)nickName
                       avatar:(NSString *)avatarStr {
    XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
    XMWeakSelf;
    [_Network getUserSigWithUserId:userModel.userId
                           success:^(id  _Nonnull reponseObject) {
        NSString *giveInfoStr = [reponseObject safeStringForKey:@"data"];
        NSString *orderType = [weakSelf.meetDic safeStringForKey:@"orderType"];
        if ([orderType isEqualToString:@"2"]) { //1.转卖 2.赠送
            [weakSelf giveToUserWithUserSigStr:giveInfoStr
                                        userId:userId
                                      nickName:nickName
                                     avatarStr:avatarStr];;
        }
        else {
            [weakSelf resellToUserWithUserSigStr:giveInfoStr
                                          userId:userId
                                        nickName:nickName
                                       avatarStr:avatarStr];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)resellToUserWithUserSigStr:(NSString *)userSigStr
                            userId:(NSString *)userId
                          nickName:(NSString *)nickName
                         avatarStr:(NSString *)avatarStr {
    XMWeakSelf;
    [_Network getResellInfoWithOrderId:self.orderId
                                 money:[self.meetDic safeStringForKey:@"price"]
                               success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
        NSString *giveInfoStr = [responseObject safeStringForKey:@"data"];
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:weakSelf.meetDic];
        [parameters safe_setObject:giveInfoStr forKey:@"giveInfo"];
        XMChatViewController *chatVC = [[XMChatViewController alloc] init];
        chatVC.sender = userModel.userId;
        chatVC.senderAvatar = userModel.avatar;
        chatVC.receiver = userId;
        chatVC.receiverName = nickName;
        chatVC.receiverAvatar = avatarStr;
        chatVC.userSigStr = userSigStr;
        chatVC.orderInfoDic = weakSelf.meetDic;
        [weakSelf.navigationController pushViewController:chatVC animated:YES];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (void)giveToUserWithUserSigStr:(NSString *)userSigStr
                          userId:(NSString *)userId
                        nickName:(NSString *)nickName
                       avatarStr:(NSString *)avatarStr {
    XMWeakSelf;
    [_Network getGiveInfoWithOrderId:self.orderId
                             success:^(id  _Nonnull responseObject) {
        XMUserModel *userModel = [[XMUserManager shareInstance] getUser];
        NSString *giveInfoStr = [responseObject safeStringForKey:@"data"];
        
        NSMutableDictionary *parameters = [NSMutableDictionary dictionaryWithDictionary:weakSelf.meetDic];
        [parameters safe_setObject:giveInfoStr forKey:@"giveInfo"];
        XMChatViewController *chatVC = [[XMChatViewController alloc] init];
        chatVC.sender = userModel.userId;
        chatVC.senderAvatar = userModel.avatar;
        chatVC.receiver = userId;
        chatVC.receiverName = nickName;
        chatVC.receiverAvatar = avatarStr;
        chatVC.userSigStr = userSigStr;
        chatVC.orderInfoDic = weakSelf.meetDic;
        [weakSelf.navigationController pushViewController:chatVC animated:YES];
        
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

- (XMFriendListView *)friendListView {
    if (!_friendListView) {
        _friendListView = [[XMFriendListView alloc] initWithFrame:self.view.bounds];
        XMWeakSelf;
        _friendListView.userSelectBlock = ^(NSString * _Nonnull userId, NSString * _Nonnull nickName, NSString * _Nonnull avatarStr) {
            [weakSelf shareToUserWithUserId:userId nickName:nickName avatar:avatarStr];
        };
    }
    return _friendListView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
